package cn.itsource.pet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.baidumap.DistanceUtil;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMessageDto;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper msgMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper opaMapper;

    @Override
    public void jiedan(SearchMessageDto dto) {

        //dto: 员工id  note描述 接单表的主键
        //1.根据接单表的主键id，查接单表的对象
        Long id = dto.getId();
        SearchMasterMsg msg = msgMapper.selectByPrimaryKey(id);

        //1.1设置为接单状态，设置shopId的值
        msg.setState(1);//接单
        msg.setShopId(GetUserInfoUtil.getShopId());

        msgMapper.updateByPrimaryKeySelective(msg);

        //2.一个个设置字段值
        OrderPetAcquisition opa = new OrderPetAcquisition();

        opa.setDigest(dto.getNote());
        opa.setState(0);
        opa.setPrice(msg.getPrice());
        opa.setUserId(msg.getUserId());
        opa.setShopId(msg.getShopId());
        opa.setAddress(msg.getAddress());
        opa.setEmpId(dto.getEmployeeId());

        //3.入库到订单表
        opaMapper.insertSelective(opa);


    }

    @Override
    public PageList<SearchMasterMsg> findMyPage(SearchMasterMsgQuery query) {
        //1.使用分页插件,因为下面要进行5km筛选，所有不能用分页插件
        //PageHelper.startPage(query.getPage(),query.getSize());

        //2.拿到店铺的id，拿到店铺的地址
        Long shopId = GetUserInfoUtil.getShopId();

        query.setShopId(shopId);
        //3.查询所有数据  10条件
        List<SearchMasterMsg> messageList = msgMapper.findMyPage(query);
        if (CollectionUtil.isEmpty(messageList)){
            return new PageList<>();
        }


        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        String address = shop.getAddress();

        //4.店铺地址要和每一条接单数据的地址进行5km的比对   8条
        //filter:按照一定规则过滤  item：msgList中的一个个对象  规则：DistanceUtil.isInCircle(address, item.getAddress(), 5000)，5km之内就为true
        List<SearchMasterMsg> msgList = messageList.stream().filter(item -> DistanceUtil.isInCircle(address, item.getAddress(), 5000))
                //collect:收集 toList：变成一个list返回
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(msgList)){
            return new PageList<>();
        }

        /*ArrayList<SearchMasterMsg> xxxx = new ArrayList<>();
        for (SearchMasterMsg item : messageList) {
            if (DistanceUtil.isInCircle(address, item.getAddress(), 5000)){
                xxxx.add(item);
            }
        }*/

        //但是进行分页，lambda分页
        //skip:跳过                                          从多少索引开始取                                    取几条
        List<SearchMasterMsg> list = msgList.stream().skip((query.getPage() - 1) * query.getSize()).limit(query.getSize()).collect(Collectors.toList());


        //4.返回自定义的PageList对象
        return new PageList<>((long)msgList.size(),list);
    }




}
